package com.mengmablog.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mengmablog.blog.DTO.ArchiveDTO;
import com.mengmablog.blog.commons.BlogConst;
import com.mengmablog.blog.commons.ServerResponse;
import com.mengmablog.blog.dao.ArticlesMapper;
import com.mengmablog.blog.entity.Articles;
import com.mengmablog.blog.service.port.IArticlesService;
import com.mengmablog.blog.utils.MarkDownUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("iArticlesService")
public class ArticlesServiceImpl implements IArticlesService {

    @Autowired
    private ArticlesMapper articlesMapper;

    @Override
    public ServerResponse article(Integer page, Integer limit) {
        //pagehelper 分页  page代表第几页，limit代表一页几条数据
        PageHelper.startPage(page, limit);
        List<Articles> list = articlesMapper.findAllArticle();
        for (Articles articles : list) {
            MarkDownUtil.transformPreView(articles);
        }
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse article_id(Integer id, String type) {
        Articles articles = articlesMapper.selectByPrimaryKey(id);
        if (type.equals("client")) {
            MarkDownUtil.transformPreView(articles);
        }
        updateHits(id);
        return ServerResponse.createBySuccess(articles);
    }

    @Override
    public ServerResponse archive() {
        int systemYear = LocalDate.now().getYear();
        List<ArchiveDTO> archiveDTOS = new ArrayList<>();
        for (int currentYear = BlogConst.CREATE_DATE; currentYear < systemYear; currentYear++) {
            String begin = currentYear + "-01-01";
            String end = currentYear + "-12-31";

            List<Articles> articlesList = articlesMapper.findAllByYear(begin, end);
            if (articlesList.size() > 0) {
                ArchiveDTO archiveDTO = new ArchiveDTO();
                archiveDTO.setDateStr(currentYear + "");
                archiveDTO.setCount(articlesList.size());
                archiveDTO.setArticles(articlesList);

                archiveDTOS.add(archiveDTO);
            }
        }
        return ServerResponse.createBySuccess(archiveDTOS);

    }

    @Override
    public ServerResponse article_count() {
        List<Articles> list = articlesMapper.findAllArticle();
        return ServerResponse.createBySuccess(list.size());
    }

    @Override
    public ServerResponse article(Integer id, String title, String content, String tags, String category, String status, Boolean allowComment) {
        Articles articles = new Articles();
        articles.setId(id);
        articles.setTitle(title);
        articles.setContent(content);
        articles.setTags(tags);
        articles.setCategory(category);
        articles.setStatus(status);
        articles.setAllowComment(allowComment);
        articles.setHits(0);
        articles.setAuthorId(1);
        articles.setType("post");
        articles.setCommentCount(0);
        if (id > 0) {
            int i = articlesMapper.updateById(id, title, content, tags, category, status, allowComment);
            if (i > 0) {
                return ServerResponse.createBySuccessMessage("修改成功");
            }
        }
        int insert = articlesMapper.insert(articles);
        if (insert > 0) {
            return ServerResponse.createBySuccessMessage("新增成功");
        }

        return ServerResponse.createByErrorMessage("失败");
    }

    @Override
    public ServerResponse delete(String id) {
        int i = articlesMapper.deleteByPrimaryKey(Integer.valueOf(id));
        if (i > 0) {
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByError();
    }

    @Override
    public ServerResponse page(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Articles> list = articlesMapper.findByType();
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse page_id(String id) {
        Articles articles = articlesMapper.selectByPrimaryKey(Integer.valueOf(id));
        return ServerResponse.createBySuccess(articles);
    }

    @Override
    public ServerResponse page_insert(Integer id, String title, String content, String status) {
        if (id == 0) {
            int j = articlesMapper.insertByPage(title, content, status);
            if (j > 0) {
                return ServerResponse.createBySuccess();
            }
        }else {
            int i = articlesMapper.updateByPage(id, title, content, status);
            if (i > 0) {
                return ServerResponse.createBySuccess();
            }
        }
        return ServerResponse.createByError();
    }

    @Override
    public ServerResponse page_delete(Integer id) {
        int i = articlesMapper.deleteByPrimaryKey(id);
        if (i>0){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }

    private void updateHits(Integer articleId) {
        articlesMapper.updateByHitsArticleId(articleId);
    }
}
